package com.easonji.attendance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.attendance.mapper.RecordMapper;
import com.easonji.attendance.mapper.RecordStateMapper;
import com.easonji.attendance.model.LeaveState;
import com.easonji.attendance.model.Record;
import com.easonji.attendance.model.RecordState;
import com.easonji.attendance.service.IRecordStateService;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.factory.PageFactory;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;
import com.easonji.core.exception.BizException;
import com.easonji.core.shiro.ShiroUser;
import com.easonji.core.shiro.util.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 补卡状态 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-10-05
 */
@Service
public class RecordStateServiceImpl extends AbstractBaseService<RecordState> implements IRecordStateService {

    @Resource
    private RecordStateMapper recordStateMapper;

    @Resource
    private RecordMapper recordMapper;

    @Override
    protected JSONObject warp2Json(RecordState recordState) {
       JSONObject result = JsonUtils.parseObject(recordState);

       return result;
    }


    @Override
    public Object queryList(BaseQuery baseQuery){

        // 分页信息
        Page<RecordState> page = new PageFactory<RecordState>().defaultPage();
        // 查询条件
        Wrapper<RecordState> wrapper = new EntityWrapper<>();

        List<RecordState> recordStates = recordStateMapper.selectPage(page, wrapper);

        return new PageInfo(warp(recordStates), page.getTotal());
    }

    @Override
    public Object queryRowById(Integer resId) {
        RecordState recordState = recordStateMapper.selectById(resId);
        return warp(recordState);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = BizException.class)
    public Integer save(RecordState recordState) {


        ShiroUser shiroUser = ShiroUtils.getUser();

        recordState.setResDatetime(new Date());

        RecordState recordState_old = new RecordState();
        BeanUtils.copyProperties(recordState,recordState_old);

        Integer insertint = recordStateMapper.updateById(recordState);

        if(insertint==0){
            return 0;
        }

        Integer otsState = 0;

        if(recordState.getResState() == 1 && recordState.getResPost()== 2){
            otsState = 1;
        }else if(recordState.getResState() == -1 && recordState.getResPost()== 2){
            otsState = -1;
        }else if(recordState.getResState() == 1 && recordState.getResPost()== 1){
            otsState = 2;
        }else if(recordState.getResState() == -1 && recordState.getResPost()== 1){
            otsState = -2;
        }


        RecordState recordState1 = queryRecordStateasc(recordState.getResRecord());

        Record record = new Record();
        Record recordForm = new Record();
        recordForm.setReState(otsState);
        recordForm.setReUpdateUser(shiroUser.getUserId());
        recordForm.setReUpdateDate(new Date());
        recordForm.setReId(recordState.getResRecord());
        if(StringUtils.isNotEmpty(recordState1)){
            recordForm.setReNextAuditor(recordState1.getResAuditor());
        }
        if(recordState.getResPost() ==1){
            recordForm.setReNextAuditor(0);
        }
        BeanUtils.copyProperties(recordForm,record);

        return recordMapper.updateById(record);
    }

    @Override
    public Integer update(RecordState recordStateForm) {
        RecordState recordState = new RecordState();
        BeanUtils.copyProperties(recordStateForm,recordState);
        return recordStateMapper.updateById(recordState);
    }

    @Override
    public Integer delete(Integer resId) {
        Wrapper<RecordState> wrapper = new EntityWrapper<>();
        wrapper.eq("res_id", resId);
        return recordStateMapper.delete(wrapper);
    }

    @Override
    public List<Integer> queryRecordIdByauditor(Integer auditor){


        Wrapper<RecordState> wrapper = new EntityWrapper<>();
        wrapper.ne("res_state",0);
        wrapper.eq("res_auditor",auditor);
        wrapper.groupBy("res_record");
        List<RecordState> recordStates = recordStateMapper.selectList( wrapper);

        List<Integer> leaves = new ArrayList<>();
        for (RecordState re :recordStates){
            leaves.add(re.getResRecord());
        }

        return leaves;
    }

    @Override
    public RecordState queryRecordStateasc(Integer oId){

        RecordState recordState = new RecordState();

        Wrapper<RecordState> wrapper = new EntityWrapper<>();

        wrapper.eq("res_record",oId);

        wrapper.eq("res_state",0);

        wrapper.orderBy("res_id");

        List<RecordState> leaveStates = recordStateMapper.selectList( wrapper);

        if(leaveStates.size()>0){
            recordState = leaveStates.get(0);
        }

        return recordState;
    }

}
